package cn.vawn.exam.service.impl;

import cn.vawn.base.dto.PageDTO;
import cn.vawn.base.dto.RequestPageDTO;
import cn.vawn.base.util.DateUtil;
import cn.vawn.base.util.StringArrayUtil;
import cn.vawn.exam.entity.Answer;
import cn.vawn.exam.entity.Questions;
import cn.vawn.exam.mapper.AnswerMapper;
import cn.vawn.exam.mapper.QuestionSerialMapper;
import cn.vawn.exam.mapper.QuestionsMapper;
import cn.vawn.exam.service.IQuestionsService;
import cn.vawn.exam.service.exception.DataNotFoundException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("questionsService")
public class QuestionsServiceImpl implements IQuestionsService
{

	@Resource
	private QuestionsMapper questionsMapper;

	@Resource
	private AnswerMapper answerMapper;

	@Resource
	private QuestionSerialMapper questionSerialMapper;

	public List<Map<String, Object>> getAllQuestions() {
		List<Map<String, Object>> questions = questionsMapper.selectAllQuestionsPart();
		return questions;
	}

	public PageDTO getPartQuestions(RequestPageDTO requestPageDTO, String content,
									String classType, String startTime,String endTime) {

		PageDTO pd = new PageDTO();

		int pageNumber = requestPageDTO.getPageNumber();
		int pageSize = requestPageDTO.getPageSize();
		String sort = requestPageDTO.getSort();
		String desc = requestPageDTO.getDesc();
		if(pageNumber > 0 &&  pageSize > 0){
			PageHelper.startPage(pageNumber, pageSize);
		}
		if(!sort.isEmpty() && !desc.isEmpty()){
			PageHelper.orderBy(sort + " " + desc);
		}

		Date startDate = null;
		if(!"".equals(startTime)) {
			startDate = DateUtil.parseDate(startTime, "yyyy-MM-dd");
		}
		Date endDate = null;
		if(!"".equals(endTime)) {
			endDate = DateUtil.parseDate(endTime, "yyyy-MM-dd");
		}

		List<Map<String, Object>> questions =
				questionsMapper.selectAllQuestionsPartByCondition(content, classType, startDate, endDate);

		pd.setPageNumber(pageNumber);
		pd.setPageSize(pageSize);
		pd.setTotal((int) new PageInfo(questions).getTotal());
		pd.setData(new PageInfo(questions).getList());

//		for(Map<String, Object> map : questions) {
//			for(Map.Entry<String, Object> entry : map.entrySet()) {
//				System.out.println(entry.getKey() + ":" + entry.getValue());
//			}
//		}

		return pd;
	}

	public void updateQuestion(Questions question) {
		//��֤�����Ƿ����
		Questions get_question = questionsMapper.selectQuestionByQuestionsId(question.getId());

		if(get_question == null) {
			throw new DataNotFoundException("数据不存在，请刷新后重试");
		}

		questionsMapper.updateByPrimaryKey(question);


	}

	public void deleteQuestion(int questionsId) {
		//
		Questions question =
				questionsMapper.selectQuestionByQuestionsId(questionsId);

		if(question == null) {
			throw new DataNotFoundException("数据不存在，请刷新后重试");
		}

		questionsMapper.deleteByQuestionsId(questionsId);
	}

	@Override
	public void addQuestion(String content, String classType, int optionType, String answerIds) {
		//
		Questions question = new Questions();

		//
		question.setAnswerIds(answerIds);
		question.setClassType(classType);
		question.setContent(content);
		question.setOptionType(optionType);
		question.setCreatedTime(new Date());

		//
		questionsMapper.insertQuestions(question);

	}

	@Override
	public List<Questions> batchQuestions(List<Integer> questionIds) {
		return questionsMapper.batchSelectQuestions(questionIds);
	}

	/****
	 * 获取所有问题的类型
	 */
	@Override
	public List<String> getAllQuestionClassType() {
		return questionsMapper.selectAllClassType();
	}

	@Override
	public PageDTO serachQuestions(RequestPageDTO requestPageDTO){

		PageDTO pd = new PageDTO();

		int pageNumber = requestPageDTO.getPageNumber();
		int pageSize = requestPageDTO.getPageSize();
		String sort = requestPageDTO.getSort();
		String desc = requestPageDTO.getDesc();
		if(pageNumber > 0 &&  pageSize > 0){
			PageHelper.startPage(pageNumber, pageSize);
		}
		if(!sort.isEmpty() && !desc.isEmpty()){
			PageHelper.orderBy(sort + " " + desc);
		}

		List<Map<String, Object>> questions =
				questionsMapper.selectAllQuestionsPart();

		pd.setPageNumber(pageNumber);
		pd.setPageSize(pageSize);
		pd.setTotal((int) new PageInfo(questions).getTotal());
		pd.setData(new PageInfo(questions).getList());

		return pd;
	}

	@Override
	public List<Questions> batchSelectQuestionsByIds(List<Integer> questionsIds) {
		return questionsMapper.batchSelectQuestions(questionsIds);
	}

	@Override
	public Map<String, Object> getQuestions(Integer questionsId) {

		Map<String, Object> questionMap = new HashedMap();

		Questions questions = questionsMapper.selectByPrimaryKey(questionsId);
		questionMap.put("questionsId", questions.getId());
		questionMap.put("content", questions.getContent());
		questionMap.put("optionType", questions.getOptionType());
		questionMap.put("classType", questions.getClassType());

		//获取题目中的所有答案选项
		String answerIds = questions.getAnswerIds();
		List<Integer> answerIdsList = StringArrayUtil.stringArrToList(answerIds.split(","));

		List<Answer> answers = answerMapper.batchSelectAnswers(answerIdsList);
		questionMap.put("answers", answers);

		return questionMap;
	}

	@Override
	public Questions getAnswers(int questionId) {
		return questionsMapper.selectAnswerIdsByQuestionsId(questionId);
	}

	@Override
	public boolean checkQuestionId(int questionsId) {
		List<Integer> questionIds = questionSerialMapper.getAllQuestionId();
		return questionIds.contains(questionsId);
	}

}
